<?php

namespace App\Http\Controllers\Admin;

use App\Models\Orm\Candy;
use App\Models\Orm\Market;
use App\Models\Orm\AwAuditRecord;
use App\Models\Orm\CooperativeCompany;
use App\Models\Orm\Rank;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

/**
 * 产品模块控制器
 *
 * Class ProductController
 * @package App\Http\Controllers\Admin
 */
class RankController extends Controller
{
    /**
     * 产品列表(展示页)
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $user = auth('admin')->user();

        $data = Rank::orderby('id', SORT_DESC)->get();
        return view('admin.rank.index', compact('data'));

    }

    public function create(Request $request)
    {
        $user = auth('admin')->user();

        if ($request->isMethod('post')) {

            $request->flash();


            // 验证数据
            // TODO(先只做了部分必要验证,详细验证后期加)
            $validator = Validator::make($request->all(), [
                'phone' => 'required|unique:' . Rank::TABLE_NAME . ',phone',


//                'crowd' => 'required|array|min:1',
//                'review' => 'array|min:1',
            ], [], [
                'phone' => '手机号',

            ]);

            if ($validator->fails()) {
                return back()->withErrors($validator);
            }
            $insert_data = $request->except(['_token','s']);
            // 用户ID
            $insert_data['admin_users_id'] = $user->id;
            $insert_data['created_at'] = date('Y-m-d H:i:s');
            $insert_data['updated_at'] = date('Y-m-d H:i:s');
//            dd($insert_data);
            Rank::create($insert_data);
            return redirect('/admin/rank/index')->with(['success' => '您的申请已提交,请耐心等等待审核']);
        }

        $data = new Rank();

        return view('admin.rank.create', compact('data'));
    }


    /**
     * 产品详情展示页
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        // 当前用户
        $user = auth('admin')->user();

        // 产品信息
        $data = Rank::find($id);

        if (!$data) {
            return back()->withErrors('未找到产品');
        }

        return view('admin.rank.show', compact('data'));
    }


    public function edit(Request $request, $id)
    {
        // 当前用户
        $user = auth('admin')->user();

        // 产品信息
        $product = Rank::find($id);

        if (!$product) {
            return back()->withErrors('未找到产品');
        }

        // post请求、提交表单
        if ($request->isMethod('post')) {

            $request->flash();

            // 验证数据
            // TODO(先只做了部分必要验证,详细验证后期加)
            $validator = Validator::make($request->all(), [
                'logo' => 'image|max:2048',
            ], [], [
                'logo' => '广告图片',
            ]);

            if ($validator->fails()) {
                return back()->withErrors($validator);
            }
            $update_data = $request->except(['_token','s']);



            // 修改时间
            $update_data['updated_at'] = date('Y-m-d H:i:s');

            // 更改产品表
            $product->update($update_data);

            return redirect('/admin/rank/show/' . $product->id)->with(['success' => '修改成功']);
        }

        $data = $product;



        return view('admin.rank.edit', compact('data'));
    }





}